<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 动态背景元素 -->
      <div class="blob"></div>

      <!-- 内容容器 -->
      <div class="card-content">
        <!-- Logo -->
        <div class="logo">
          <img alt="Logo" class="logo-svg" src="@/asserts/panda.svg"/>
        </div>

        <h2 class="title">日常工具</h2>
        <p class="subtitle">请输入您的账户信息以继续</p>

        <form class="form" @submit.prevent="handleLogin">
          <!-- 用户名 -->
          <div class="input-group">
            <i class="icon-user"></i>
            <input
                v-model="username"
                :class="{ 'input-focus': isUsernameFocused }"
                autocomplete="username"
                class="input"
                placeholder="用户名或邮箱"
                required
                type="text"
                @blur="isUsernameFocused = false"
                @focus="isUsernameFocused = true"
            />
          </div>

          <!-- 密码（关键修复区） -->
          <div class="input-group">
            <i class="icon-lock"></i>
            <!-- 密码输入框 -->
            <input
                ref="passwordInputRef"
                v-model="password"
                :class="{ 'input-focus': isPasswordFocused }"
                :type="passwordVisible ? 'text' : 'password'"
                autocomplete="current-password"
                class="input"
                placeholder="密码"
                required
                @blur="isPasswordFocused = false"
                @focus="isPasswordFocused = true"
            />
            <!-- ✅ 修复：眼睛图标绝对定位，浮在输入框之上 -->
            <span
                :title="passwordVisible ? '隐藏密码' : '显示密码'"
                class="icon-eye-toggle"
                @click="togglePasswordVisibility"
            >
              {{ passwordVisible ? '🙈' : '👁️' }}
            </span>
          </div>
          <button :disabled="loading" class="btn-login" type="submit">
            {{ loading ? '登录中...' : '立即登录' }}
          </button>
        </form>
      </div>
    </div>

    <div class="background-decoration"></div>
  </div>
</template>

<script lang="ts" setup>
import {ref, nextTick} from 'vue'
import {useRouter} from 'vue-router'
import {message} from "@/utils/ElmessasgeUtils";
import {setUserInfo} from "@/utils/system";
import {currentPath} from "@/views/documentManager/components/ts/FileBrowser";
import {login} from '@/api/system/sysUser';
import {sysInit} from "@/main";

const router = useRouter()

// 表单数据
const username = ref('')
const password = ref('')
const loading = ref(false)
const passwordVisible = ref(false)

// 聚焦状态
const isUsernameFocused = ref(false)
const isPasswordFocused = ref(false)

// 🔁 修复：使用 ref 获取 input 元素
const passwordInputRef = ref<HTMLInputElement | null>(null)

// ✅ 修复：确保切换 type 后保持焦点
const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value
  // 切换后尝试保持焦点
  nextTick(() => {
    if (passwordInputRef.value) {
      passwordInputRef.value.focus()
    }
  })
}



async function handleLogin() {

  if (!username.value || !password.value) return

  loading.value = true
  let res: any = await login({username: username.value, password: password.value})

  if (res.data.code !== 200) {
    loading.value = false
    message(res.data.msg, "error")
  } else {
    setUserInfo(res.data.data);

    // 系统初始化
    await sysInit();

    // 文档浏览器的当前路径还原
    currentPath.value = ''
    loading.value = false
    await router.push('/')
  }

}

</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  font-family: 'Segoe UI', Roboto, sans-serif;
  position: relative;
  overflow: hidden;
  padding: 1rem;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(circle at 10% 20%, rgba(100, 180, 255, 0.1) 0%, transparent 20%),
  radial-gradient(circle at 90% 80%, rgba(255, 160, 122, 0.1) 0%, transparent 25%);
  pointer-events: none;
  z-index: 0;
}

.login-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  min-height: 500px;
  border-radius: 20px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
  animation: slideUp 0.6s ease-out;
}

.card-content {
  position: relative;
  z-index: 2;
  width: calc(100% - 10px);
  height: calc(100% - 10px);
  padding: 3rem 2.5rem;
  background: rgba(255, 255, 255, .95);
  backdrop-filter: blur(24px);
  border-radius: 16px;
  overflow: hidden;
  outline: 1px solid white;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.blob {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background-color: rgba(102, 126, 234, 0.6);
  opacity: 1;
  filter: blur(12px);
  animation: blob-bounce 5s infinite ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes blob-bounce {
  0% {
    transform: translate(-100%, -100%) translate3d(0, 0, 0);
  }

  25% {
    transform: translate(-100%, -100%) translate3d(100%, 0, 0);
  }

  50% {
    transform: translate(-100%, -100%) translate3d(100%, 100%, 0);
  }

  75% {
    transform: translate(-100%, -100%) translate3d(0, 100%, 0);
  }

  100% {
    transform: translate(-100%, -100%) translate3d(0, 0, 0);
  }
}

.logo {
  margin-bottom: 1.5rem;
}

.logo-svg {
  display: inline-block;
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 18px;
  line-height: 70px;
  font-size: 28px;
  color: white;
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.title {
  font-size: 1.8rem;
  color: #1f2937;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.subtitle {
  color: #6b7280;
  font-size: 1rem;
  margin-bottom: 2rem;
}

/* 输入框组 */
.input-group {
  position: relative;
  margin-bottom: 1.5rem;
}

.input-group i {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  color: #9ca3af;
  width: 30px;
  text-align: center;
  pointer-events: none;
}

.icon-user {
  left: 14px;
}

.icon-lock {
  left: 14px;
}

.input {
  width: 100%;
  padding: 14px 14px 14px 50px;
  border: 1px solid #d1d5db;
  border-radius: 12px;
  font-size: 1rem;
  transition: all 0.3s ease;
  outline: none;
  background-color: #f9fafb;
  z-index: 1; /* 输入框在底层 */
  position: relative;
}

.input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  background-color: white;
}

.input-focus {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

/* ✅ 修复：眼睛图标绝对定位，浮在输入框之上 */
.icon-eye-toggle {
  position: absolute;
  top: 50%;
  right: 14px;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  cursor: pointer;
  z-index: 3; /* 最高层 */
  border: 1px solid #e5e7eb;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  pointer-events: auto; /* 确保能点击 */
  transition: all 0.2s ease;
}

.icon-eye-toggle:hover {
  background: #f3f4f6;
  transform: translateY(-50%) scale(1.1);
}

/* 其他样式保持不变... */
.form-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  font-size: 0.95rem;
}

.checkbox {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #6b7280;
}

.checkbox input {
  display: none;
}

.checkmark {
  width: 16px;
  height: 16px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  margin-right: 8px;
  transition: all 0.2s;
}

.checkbox input:checked + .checkmark {
  background-color: #667eea;
  border-color: #667eea;
}

.checkbox input:checked + .checkmark::after {
  content: '✓';
  color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 10px;
}

.forgot-link {
  color: #667eea;
  text-decoration: none;
}

.forgot-link:hover {
  text-decoration: underline;
}

.btn-login {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-login:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.25);
}

.btn-login:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.social-login {
  margin-top: 2rem;
}

.divider {
  display: block;
  margin: 1.2rem 0;
  color: #9ca3af;
  font-size: 0.9rem;
  position: relative;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 45%;
  height: 1px;
  background-color: #e5e7eb;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.social-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: bold;
  color: white;
}

.social-btn.google {
  background: #ea4335;
}

.social-btn.github {
  background: #333;
}

.social-btn.wechat {
  background: #2ecc71;
}

.social-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.register-tip {
  text-align: center;
  margin-top: 1.5rem;
  color: #6b7280;
  font-size: 0.95rem;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-card {
    padding: 2rem 1.5rem;
    border-radius: 16px;
  }

  .input {
    padding: 12px 12px 12px 46px;
  }

  .btn-login {
    padding: 12px;
  }
}
</style>